package com.fs.book.service.impl;

import com.fs.book.entity.User;
import com.fs.book.exception.UsernameAndPasswordNotExsitException;
import com.fs.book.mapper.UserMapper;
import com.fs.book.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
   @Autowired
   private UserMapper userMapper;
    @Override
    public User login(String username, String password) throws Exception {
        //1.调用UserMapper根据用户名查询用户
        User user = userMapper.selectByName(username);
        if(user != null && user.getLoginpass().equals(password)){//登录成功
            return user;
        }
        //登录失败, 抛异常
        throw new UsernameAndPasswordNotExsitException("用户名或者密码错误");
    }

    /**
     * 注册逻辑
     * @param user
     * @return
     */
    @Override
    public int register(User user) {
        try {
            int userId = userMapper.InsertById(user);
            return userId;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("你注册失败：" + e.getMessage());
        }
    }


}
